#!/bin/bash
gpioset gpiochip2 28=0
sleep 0.1
gpioset gpiochip2 28=1

./leds all off
./leds -t 1000 2 &
pid_leds=$!

brcm_patchram_plus -d --enable_hci --no2bytes --tosleep 200000 --baudrate 3000000 --patchram /lib/firmware/BCM43013A0_001.001.006.1073.1102.hcd /dev/ttyS4 &
sleep 30
for i in {0..5}; 
do
        hciconfig hci0 down
        sleep 0.5
        hciconfig hci0 up
        hcitool scan | grep :
        if [ $? -eq 0 ];then
                kill -9 "$pid_leds"
                ./leds 2 off
                echo "BT Test PASS"
                exit 0
        else
                # kill -9 "$pid_leds"
                # ./leds 2 off
                # echo "BT Test FAIL"
                # exit 2
                sleep 0.5
        fi
done
kill -9 "$pid_leds"
./leds 2 off
echo "BT Test FAIL"
exit 2